#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python_learning 
@File ：原型注册模式.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2024/3/2 0:00 
"""

"""
原型注册模式（Prototype Registry Pattern）
模式解释：
原型注册模式是一种创建型模式，它使用原型实例注册表来避免每次实例化对象时都显式调用构造函数。
"""

import copy


class Prototype:
	def clone(self):
		pass


class ConcretePrototypeA(Prototype):
	def __init__(self, value):
		self._value = value

	def clone(self):
		return copy.deepcopy(self)


class ConcretePrototypeB(Prototype):
	def __init__(self, array):
		self._array = array

	def clone(self):
		return copy.deepcopy(self)


class PrototypeRegistry:
	def __init__(self):
		self._prototypes = {}

	def register(self, name, prototype):
		self._prototypes[name] = prototype

	def clone(self, name):
		if name in self._prototypes:
			return self._prototypes[name].clone()
		else:
			raise ValueError("Prototype not registered.")


registry = PrototypeRegistry()
prototype_a = ConcretePrototypeA(1)
prototype_b = ConcretePrototypeB([1, 2, 3])

registry.register("PrototypeA", prototype_a)
registry.register("PrototypeB", prototype_b)

clone_a = registry.clone("PrototypeA")
clone_b = registry.clone("PrototypeB")

print(clone_a.__dict__)
print(clone_b.__dict__)
